<?php

namespace App\Providers;

use App\Models\Article;
use App\Models\Category;
use App\Models\Tag;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        //解决数据库迁移时生成users表报错
        Schema::defaultStringLength(191);
        /**
         * 增加内存防止中文分词报错
         */
        ini_set('memory_limit', "256M");
        //分配前台通用的数据
        view()->composer('layouts/home', function($view){
               //标签和分类下的文章数
               $tags=Tag::has('article')->withCount('article')->get();
//               var_dump($tags);exit;
               $cats=Category::select("id","name")->withCount('article')->orderBy("sort")->limit(4)->get();
               //点击排行
               $rangeArcticle=Article::select("id","title")->orderBy("click","desc")->limit(8)->get();

               $view->with(compact('tags','cats','rangeArcticle'));
        });
    }

    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
